package com.muyaxuan.mail.controller.customer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.muyaxuan.mail.common.WebConstant;
import com.muyaxuan.mail.dao.GoodsDao;
import com.muyaxuan.mail.dao.GoodsTypeDao;
import com.muyaxuan.mail.dao.OrderDao;
import com.muyaxuan.mail.dao.OrderItemDao;
import com.muyaxuan.mail.dto.Goods;
import com.muyaxuan.mail.dto.GoodsType;
import com.muyaxuan.mail.dto.Order;
import com.muyaxuan.mail.dto.OrderItem;
import com.muyaxuan.mail.dto.User;

@Controller
@RequestMapping("/customer")
public class OrderAlipayController {
	GoodsDao goodsDao = null;
	OrderItemDao orderItemDao = null;
	OrderDao orderDao = null;
	Order order = null;
	OrderItem orderItem = null;
	GoodsTypeDao goodsTypeDao= null;
	
	/**
	 * 跳转订单页面
	 * @param ids
	 * @param data
	 * @return
	 */
	@RequestMapping("/showOrder")
	public String showOrder(String ids,HttpSession session){
		//商品总价
		double totalMoney = 0.0;
		List<Goods> goodsList = new ArrayList<>();
		//获取购物车
		Map<Integer,Integer> shopCar = (Map<Integer,Integer>)session.getAttribute(WebConstant.SHOP_CAR);
		//获取选中商品的ID
		String[] ides = ids.split(",");
		//新建商品dao
		goodsDao = new GoodsDao();
		//判断购物车是否存在
		if(shopCar!=null){
			for(String id:ides){
				//获取商品的购买数量 
				int buyNum = shopCar.get(Integer.valueOf(id));
				//获取商品的详细数据
				Goods goods = goodsDao.getGoodsById(Integer.valueOf(id));
				//将购买数量设置到商品中
				goods.setBuyNum(buyNum);
				//
				goodsList.add(goods);
				//添加总金额
				totalMoney+=goods.getBuyMoney();
			}
		}
		
		session.setAttribute("goodsList", goodsList);
		session.setAttribute("totalMoney", totalMoney);
		return "order";
	}
	/**
	 * 去支付页面
	 */
	@RequestMapping("showPayment")
	public String showPayment(int id,Model data){
		orderDao = new OrderDao();
		Order order = orderDao.getOrderById(id);
		data.addAttribute("order", order);
		return "payment";
	}
	/**
	 * 去支付页面
	 */
	@RequestMapping("payAjax")
	@ResponseBody
	public Map<String,Object> payAjax(String password,int id){
		Map<String,Object> data = new HashMap<>();
		if("1,2,3,4,5,6".equals(password)){
			order = new Order();
			order.setId(id);
			order.setSendDate(new Date());
			String tip = orderDao.payOrder(order);
			data.put("status", 0);
		}else{
			
			data.put("tip", "支付失败!");
		}
		return data;
	}
	/**
	 * 跳转订单详情页面
	 * @param alipay 0 未付款
	 * @param session
	 * @param data
	 * @return
	 */
	@RequestMapping("/showOrderDetails")
	public String showOrderDetails(int alipay,HttpSession session,Model data){
		orderDao = new OrderDao();
		User user = (User)session.getAttribute(WebConstant.SESSION_USER);
		Map<String,Object> params = new HashMap<>();
		params.put("user", user);
		params.put("alipay", alipay);
		List<Order> orderList = orderDao.getOrderByUserId(params);
		data.addAttribute("orderList", orderList);
		return "order_details";
	}
	/**
	 * 保存订单
	 * @param ids
	 * @param session
	 * @return
	 */
	@RequestMapping("/addOrder")
	public String addOrder(HttpSession session,Model model){
		//获取总金额
		double totalMoney = (double)session.getAttribute("totalMoney"); 
		//获取商品列表
		List<Goods> goodsList = (List<Goods>)session.getAttribute("goodsList");
		//获取购物车
		Map<Integer,Integer> shopCar = (Map<Integer,Integer>)session.getAttribute(WebConstant.SHOP_CAR);
		//获取用户的id
		User user = (User)session.getAttribute(WebConstant.SESSION_USER);
		
		int userId = user.getId();
		
		//设置订单
		order = new Order();
		order.setUser(user);
		order.setCreateDate(new Date());
		order.setTradingStatus(0);
		order.setOrderCode(createOrderCode(userId));
		order.setTotalAmount(totalMoney);
		orderDao = new OrderDao();
		orderDao.save(order);
		//遍历商品
		for(Goods goods:goodsList){
			goodsDao = new GoodsDao();
			goodsTypeDao = new GoodsTypeDao();
			//减少物品在数据库中的数量
			goods.setStorage(goods.getNewStorage());
			
			GoodsType goodsType = goods.getGoodsType();
			goodsType.setSale(goods.getBuyNum());
			goodsTypeDao.updateGoodsType(goodsType);
			
			goodsDao.updateGoodsById(goods);
			//添加订单详情
			orderItem = new OrderItem();
		    orderItem.setGoods(goods);
			orderItem.setOrderNum(goods.getBuyNum());
			orderItem.setOrder(order);
			orderItemDao = new OrderItemDao();
			orderItemDao.save(orderItem);
			shopCar.remove(goods.getId());
		}
		model.addAttribute("order", order);
		session.setAttribute("shopCar", shopCar);
		session.removeAttribute("goodsList");
		session.removeAttribute("totalMoney");
		return "payment";
	}

	/**
	 * 生成订单编号
	 * @param userId
	 * @return
	 */
	private String createOrderCode(int userId){
		SimpleDateFormat sdf = new  SimpleDateFormat("yyyyMMddHHmmss");
		return "MYX_"+userId+"_"+sdf.format(new Date());
	}

}
